﻿using System.Collections;
using UnityEngine;

namespace COC.Tools.Pathing2
{
    public class MovePath:MonoBehaviour
    {

    [HideInInspector]
    public AIPathfinding findingPath;
    [HideInInspector]
    public int id=0;

    public float mTileSize;
    public float mOffset;
    public float startingX;
    public float startingZ;
    private Vector3 origin;
    public float speed =.06f;
    private int step = 0;
    Transform clone;
    void Start()
    {
         origin= transform.localPosition;
       
         StartCoroutine(UpdatePosition(findingPath,origin));
    }
        void Update()
    {
        
    }
        IEnumerator UpdatePosition(AIPathfinding findingPath,Vector3 origin)
        {
            while (true)
            {

                if (findingPath != null && findingPath.followersPathInfo[id].STATE != PathInfo.PathState.NONE && findingPath.followersPathInfo[id].location > 0)
                {
                    Vector3 targetPos = findingPath.ReadPath(id, origin.x, origin.z);
                    UnityEngine.Debug.Log("STEP:" + (step++) + "@@@@@@@@@@@@@>>>>targetPosition:[" + targetPos.x + "," + targetPos.y + "," + targetPos.z + "] <<<<@@@@@@@@@@@@@");
                    float distTotal = (targetPos - origin).magnitude;

                    if (targetPos != null && targetPos != Vector3.zero && origin != targetPos)
                        this.gameObject.transform.localPosition = Vector3.Lerp(origin, targetPos, speed * Time.smoothDeltaTime);                   
                    origin = targetPos;
                   
                    
                }  
            yield return new WaitForSeconds(.7f);
            }
            yield return null;
        }

        void Destroy()
        {
            StopCoroutine("UpdatePosition");
        }


    }

}
